﻿<?php

/**
 * @Author: LauEL
 * @Date:   2020-11-19 09:02:37
 * @Email:   11447474@qq.com
 * @Last Modified by:   Administrator
 * @Last Modified time: 2020-11-19 11:04:26
*/

namespace tool;

//pc生成带参数的二维码
use think\Db;
use think\facade\Cache;
use think\facade\Request;

class WapQrCode
{

	/**
	 * [createQrCode 生成一个带参数的二维码]
	 * @param  [type] $users_id [description]
	 * @return [type]           [description]
	 */
	public function createQrCode($users_id)
	{
		
		// 生成二维码
		require "../vendor/phpqrcode/phpqrcode.php";

		$value = $url = config("app.citrix_web_path").url('home/login/publish',['users_id'=>$users_id]);   
		$errorCorrectionLevel = 'H';  //容错级别
		$matrixPointSize = 4;      //生成图片大小

		$filename = citrixHoldCode(6).time().rand(0000,9999).'.png';
		$path = $this->createDir().$filename;

		\QRcode::png($value,$path , $errorCorrectionLevel, $matrixPointSize, 2);

		$logo = dirname(dirname(__DIR__)).DS.'public'.Db::name("system")->where(['name'=>'qr_code'])->value("value"); 
		$logo = citrixParaphrase($logo);

		$QR = $path;      //已经生成的原始二维码图

		if (file_exists($logo)) 
		{
			$QR = imagecreatefromstring(file_get_contents($QR));    //目标图象连接资源。
			$logo = imagecreatefromstring(file_get_contents($logo));  //源图象连接资源。
			$QR_width = imagesx($QR);      //二维码图片宽度
			$QR_height = imagesy($QR);     //二维码图片高度
			$logo_width = imagesx($logo);    //logo图片宽度
			$logo_height = imagesy($logo);   //logo图片高度
			$logo_qr_width = $QR_width / 4;   //组合之后logo的宽度(占二维码的1/5)
			$scale = $logo_width/$logo_qr_width;  //logo的宽度缩放比(本身宽度/组合后的宽度)
			$logo_qr_height = $logo_height/$scale; //组合之后logo的高度
			$from_width = ($QR_width - $logo_qr_width) / 2;  //组合之后logo左上角所在坐标点
			imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
		}

		$filename2 = citrixHoldCode(6).time().rand(0000,9999).'.png';
		$path2 = $this->createDir().$filename2;

		//输出图片
		imagepng($QR, $path2);
		imagedestroy($QR);
		imagedestroy($logo);
		
		return citrixParaphrase(DS.'qrcode'.DS.'wap'.DS.date("Y-m-d").DS.$filename2);

	}

	/**
	 * [createDir 创建多级目录]
	 * @param  [type] $path [description]
	 * @return [type]       [description]
	 */
	public function createDir()
	{
		$path = citrixParaphrase(dirname(dirname(__DIR__)).DS.'public'.DS.'qrcode'.DS.'wap'.DS.date("Y-m-d").DS);
		if (is_dir($path))
		{  
	        return $path;
	    }
		mkdir(iconv("UTF-8", "GBK", $path),0777,true);
		return $path;
	}

}